//
//  XHomeCyclePagerCell.m
//  Bees
//
//  Created by Xiaohan on 2018/12/17.
//  Copyright © 2018 xiaohan. All rights reserved.
//

#import "XHomeCyclePagerCell.h"
#import "TYCyclePagerView.h"
#import "TYPageControl.h"
#import "XHomeAdClvCell.h"
#import "FoundModel.h"
static  NSString*const kXCellId=@"kCellId";
@interface XHomeCyclePagerCell() <TYCyclePagerViewDataSource, TYCyclePagerViewDelegate>
@property (nonatomic, weak) TYCyclePagerView *pagerView;
@property (nonatomic, strong) UIView *bottomV;
//@property (nonatomic, strong) TYPageControl *pageControl;
@end

@implementation XHomeCyclePagerCell

- (void)setData:(NSArray<FoundBannerModel*>*)data{
    _data = data;
    [self.pagerView reloadData];
}
- (void)setupUI{
    [self addPagerView];
//    [self addOtherView];
}
- (void)addOtherView{
    self.bottomV = [UIView new];
    [self.contentView addSubview:self.bottomV];
    [self.bottomV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.pagerView.mas_bottom);
        make.left.bottom.right.mas_equalTo(0);
    }];
    [self.bottomV addSubview:self.imageV];
    [self.imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(AdaptedWidth(3.0f));
        make.left.mas_equalTo(kNormalSpace);
        make.bottom.mas_equalTo(-AdaptedWidth(10.0f));
        make.width.equalTo(self.imageV.mas_height).multipliedBy(1.0f);
    }];
    [self.bottomV addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imageV.mas_right).offset(AdaptedWidth(11.0f));
        make.top.equalTo(self.imageV.mas_top).offset(AdaptedWidth(10.0f));
    }];
    [self.bottomV addSubview:self.subLab];
    [self.subLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imageV.mas_right).offset(AdaptedWidth(11.0f));
        make.bottom.equalTo(self.imageV.mas_bottom).offset(AdaptedWidth(-10.0f));
    }];
    [self.bottomV addSubview:self.scoreTipLab];
    [self.scoreTipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLab.mas_centerY);
        make.right.mas_equalTo(AdaptedWidth(-16.0f));
    }];
    [self.bottomV addSubview:self.scoreLab];
    [self.scoreLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.subLab.mas_centerY);
        make.centerX.equalTo(self.scoreTipLab.mas_centerX);
    }];
}
- (void)addPagerView {
    TYCyclePagerView *pagerView = [[TYCyclePagerView alloc]initWithFrame:CGRectZero];
    pagerView.isInfiniteLoop = YES;
    pagerView.autoScrollInterval = 0;
    pagerView.dataSource = self;
    pagerView.delegate = self;
    [pagerView registerClass:[XHomeAdClvCell class] forCellWithReuseIdentifier:kXCellId];
    [self.contentView addSubview:pagerView];
    [pagerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.top.mas_equalTo(0);
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
//        make.height.mas_equalTo(AdaptedWidth(140.0f));
    }];
    _pagerView = pagerView;
}

#pragma mark - TYCyclePagerViewDataSource

- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    return self.data.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    XHomeAdClvCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:kXCellId forIndex:index];
    [cell.imgView sd_setImageWithURL:String_To_URL([self.data objectAtIndex:index].image) placeholderImage:nil];
    return cell;
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
    layout.itemSize = pageView.size;/*  CGSizeMake(CGRectGetWidth(pageView.frame)*0.90, CGRectGetHeight(pageView.frame)*0.95);*/
    layout.itemSpacing = AdaptedWidth(8);
    layout.minimumAlpha = 0.3;
    layout.itemHorizontalCenter = YES;
    layout.layoutType = TYCyclePagerTransformLayoutNormal;
    layout.isInfiniteLoop = NO;
    return layout;
}

- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
//    _pageControl.currentPage = toIndex;
//    [UIView transitionWithView:self.bottomV duration:0.8f options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
//        [self.imageV sd_setImageWithURL:[NSURL URLWithString:[self.data objectOrNilAtIndex:toIndex]] placeholderImage:nil];
//        self.titleLab.text = [NSString stringWithFormat:@"野蛮世界%ld",toIndex];
//        self.subLab.text = [NSString stringWithFormat:@"点点手指跟上丛林的节奏%ld",toIndex];
//        self.scoreLab.text = [NSString stringWithFormat:@"%.1f",toIndex+8.8];;
//    } completion:nil];
}
- (void)pagerView:(TYCyclePagerView *)pageView didSelectedItemCell:(__kindof UICollectionViewCell *)cell atIndex:(NSInteger)index{
    !self.ClickBlock?:self.ClickBlock([self.data objectAtIndex:index]);
}

- (UIImageView*)imageV{
    if (!_imageV) {
        _imageV = [UIImageView new];
        _imageV.contentMode = UIViewContentModeScaleAspectFill;
        ViewRadius(_imageV, AdaptedWidth(13.0f));
    }
    return _imageV;
}

- (UILabel*)titleLab{
    if (!_titleLab) {
        _titleLab = [UILabel new];
        _titleLab.font = kBoldFont(16);
        _titleLab.numberOfLines = 1;
    }
    return _titleLab;
}
- (UILabel*)subLab{
    if (!_subLab) {
        _subLab = [UILabel new];
        _subLab.font = kDefaultFont(12);
        _subLab.numberOfLines = 1;
        _subLab.textColor = kColorTextGray;
    }
    return _subLab;
}

- (UILabel*)scoreTipLab{
    if (!_scoreTipLab) {
        _scoreTipLab = [UILabel new];
        _scoreTipLab.numberOfLines = 1;
        _scoreTipLab.textColor = kColorTextGray;
        NSMutableAttributedString*tip = [[NSMutableAttributedString alloc]initWithString:XLocalized(@"Bees 评分")];
        tip.font = kBoldFont(12);
        [tip setColor:UIColorHex(#FFCC03) range:NSMakeRange(0, 4)];
        _scoreTipLab.attributedText = tip;
    }
    return _scoreTipLab;
}

- (UILabel*)scoreLab{
    if (!_scoreLab) {
        _scoreLab = [UILabel new];
        _scoreLab.numberOfLines = 1;
        _scoreLab.textColor = kTitleColor;
        _scoreLab.font = kBoldFont(12);
        _scoreLab.textAlignment = NSTextAlignmentCenter;
    }
    return _scoreLab;
}
@end
